import './assets/main.css'
import 'vant/lib/index.css';
import App from './App.vue'
import {createApp, watch} from 'vue'
import s94 from "s94-js";
import {get_os, s94_log} from "@/js/s94_tool.js";
import s94_rem from "@/js/s94_rem.mjs.js";
import router_auto from './vue_plugin/router_auto.js'
import i18n from './vue_plugin/i18n/index.js'
import Vant from 'vant'
import {use_config_map} from "@/store/config_map.js";
import pluginCache from "@/store/pluginCache.js";
import {createPinia} from "pinia";
import {isDebug} from "@/js/debug_tool.js";

// ---------------------- 初始化 ----------------------
//版本
const VERSION = '1.2.1';
console.log(`VERSION=${VERSION}`);
//设备类型
s94.load.os = get_os();
//rem，设备宽度=(base/em)rem，设备的宽度大于600后
s94_rem.setScale(0.8).display(600, 160); // 设备宽度等于3.75rem


// ---------------------- 页面构建 ----------------------
const vue = createApp(App)
// 加载vue插件
const pinia = createPinia()
pinia.use(pluginCache);
vue.use(pinia)
vue.use(router_auto);
vue.use(Vant);

// 国际化
vue.use(i18n);


let app = vue.mount('#app');
window["app"] = app;

// 调试模式
if(isDebug()){
    // 错误事件绑定
    vue.config.errorHandler = function (err, vm, info){
        console.error(err);
        s94_log(err.stack, info);
    }
    window.addEventListener('error', (evt)=>{
        console.error(evt.error?.message || evt.message);
        if (typeof evt.error) {
            s94_log(evt.error.stack, evt.error.message);
        }
    })
}

//页面加载
if ("loading" == document.readyState) {
    document.addEventListener("DOMContentLoaded", function (){
        s94.load.init(s94.load.os)
    })
}else {
    s94.load.init(s94.load.os);
}
// 版本比较
// 判断版本，如果版本小于指定版本，执行 cache_load.init(true)
if (typeof cache_load === 'function' && typeof cache_load.init === 'function'){
    let version_arr = s94.to_string(use_config_map().config.system?.vue_code_version).split('.');
    if (VERSION.split('.').some((v,i)=>{ return parseInt(v) < parseInt(version_arr[i]); })) cache_load.init(true);
}

watch(()=>use_config_map().this_tenant, (v, oldV)=>{
    console.log('this_tenant', v, oldV)
    s94(function(){
        //标题和图标
        document.title = v.title;
        document.querySelector('link[rel="icon"]').setAttribute('href', v.head_img)
    })
})

export default app;